#include "InterfaceArchiver.h"

InterfaceArchiver::InterfaceArchiver()
{
	    ////////////////
    // Compression
    qDebug() << "Compression:";

    // Compress a single file
    qDebug() << "Compression of main.cpp in tmp/example1.zip:" \
             << JlCompress::compressFile("tmp/example1.zip","./libgcc_s_dw2-1.dll");
	
	
	CurrentOutputFilePath = "";
	qWarning() << "Info : Archiver Interface starting...";
#define ARCHIVER_7zip
	#ifdef ARCHIVER_7zip
		qWarning() << "Info :     >> External archiver selected : 7zip";
		Archiver_sevenZip = new QProcess;
        connect(Archiver_sevenZip, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(CompressFinished(int,QProcess::ExitStatus)));

	#elif ARCHIVER_zip
		qWarning() << "Warning :    >> INVALID OPTION : Zip. Skipped";
	#elif ARCHIVER_targz
		qWarning() << "Warning :    >> INVALID OPTION : tar.gz. Skipped";
	#else 
		qWarning() << "Warning :    >> No Archiver selected!";
	#endif
	qWarning() << "Info : Archiver Interface ready";
}
InterfaceArchiver::~InterfaceArchiver()
{
	qWarning() << "Info : Archiver Interface stoped";
	#ifdef ARCHIVER_7zip
		delete Archiver_sevenZip;
	#endif
}
bool InterfaceArchiver::CheckAndDeleteOldArchive(QString AbsoluteFilePath)
{
	QFile File;
	File.setFileName(AbsoluteFilePath);
	//Ajout de l'extension de fichier adéquate
	#ifdef ARCHIVER_7zip
		File.setFileName(File.fileName() + ".7z");
	#elif ARCHIVER_zip
		//qWarning() << "    >> INVALID OPTION : Zip. Skipped";
        File.setFileName(File.fileName() + ".zip");
	#elif ARCHIVER_targz
		//qWarning() << "    >> INVALID OPTION : tar.gz. Skipped";
	#else 
		//qWarning() << "    >> No Archiver selected!";
	#endif
	if(File.exists())
	{
		if(!File.remove())
		{
			qDebug() << "Warning : Failled to create path :" << File.fileName();
			return false;
		}
	}
	return true;
}

void InterfaceArchiver::CreateArchive(QString InputFilePath,QString OutputFilePath)
{
	#ifdef ENABLE_DEBUG_ARCHIVER_CREATEARCHIVE
		qDebug() << __FILE__ << ":" << __FUNCTION__ << ":" << __LINE__
		         << "Input =" << InputFilePath;
		qDebug() << __FILE__ << ":" << __FUNCTION__ << ":" << __LINE__
		         << "Output =" << OutputFilePath;
    #endif
	#ifdef ARCHIVER_7zip
        #ifdef Q_OS_UNIX
			//qDebug() << "OS : GNU/Linux";
			CurrentOutputFilePath = OutputFilePath;
            Archiver_sevenZip->start("7z a " + OutputFilePath + ".7z " + InputFilePath);
			if(!Archiver_sevenZip->waitForStarted())
			{
				emit ArchiveCreateEnd(OutputFilePath, false);
			}
		#endif
        #ifdef Q_OS_WIN32
            //qDebug() << "OS :Windows";
            CurrentOutputFilePath = OutputFilePath;
            Archiver_sevenZip->start("7z a " + OutputFilePath + ".7z " + InputFilePath);
            if(!Archiver_sevenZip->waitForStarted())
            {
                emit ArchiveCreateEnd(OutputFilePath, false);
            }
        #endif
		#ifdef Q_OS_MAC
			qWarning() << "Warning : INVALID OPERATIVE SYSTEM : no supported yet"; 
			emit ArchiveCreateEnd(OutputFilePath, false);
		#endif
	#elif ARCHIVER_zip
		qWarning() << "Warning :    >> INVALID OPTION : Zip. Skipped";
		emit ArchiveCreateEnd(OutputFilePath, false);
	#elif ARCHIVER_targz
		qWarning() << "Warning :    >> INVALID OPTION : tar.gz. Skipped";
		emit ArchiveCreateEnd(OutputFilePath, false);
	#else 
		qWarning() << "Warning :    >> No Archiver selected!";
		emit ArchiveCreateEnd(OutputFilePath, false);
	#endif
}
void InterfaceArchiver::CompressFinished( int exitCode, QProcess::ExitStatus exitStatus )
{
	if(exitStatus == QProcess::NormalExit)
	{
		emit ArchiveCreateEnd(CurrentOutputFilePath, true);
	}
	else
	{
		emit ArchiveCreateEnd(CurrentOutputFilePath, false);
	}
}
